package com.file.domain;

public enum FileTypeEnum {
    IMAGE(1, "image"),
    VIDEO(2, "video"),
    FILE(3, "file");

    private final int code;        // 存数据库时的数字 1、2、3
    private final String path;     // 对应OSS存储路径前缀

    FileTypeEnum(int code, String path) {
        this.code = code;
        this.path = path;
    }

    public int getCode() {
        return code;
    }

    public String getPath() {
        return path;
    }

    // 通过code找枚举
    public static FileTypeEnum fromCode(int code) {
        for (FileTypeEnum type : values()) {
            if (type.code == code) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知文件类型 code: " + code);
    }

    // 通过文件后缀名判断类型
    public static FileTypeEnum fromSuffix(String suffix) {
        suffix = suffix.toLowerCase();
        if (suffix.matches("jpg|jpeg|png|gif|bmp|webp")) {
            return IMAGE;
        } else if (suffix.matches("mp4|avi|mov|mkv|flv")) {
            return VIDEO;
        } else {
            return FILE;
        }
    }
}
